<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class LinkController extends Controller
{
    //show
    public function show(Request $request)
    {
    	$title = "友情链接";

    	//会话控制
        $staff = $request->session()->get('staff');


    	$data = DB::table('bro_link') -> orderBy('order','asc')->where(function($query) use ($request){
            $query -> where('linkname','like','%'.$request -> input('keywords').'%');
        }) -> paginate($request -> input('num',10));
        return view('admin.conf.link.show',['title' => $title,'data' => $data,'request' => $request -> all(),'staff' => $staff]);
    }

    //add
    public function add(Request $request)
    {
    	$title = "友情链接";

        //会话跟踪
        $staff = $request->session()->get('staff');

    	return view('admin.conf.link.add',['title' => $title,'staff' => $staff]);
    }

    //insert
    public function insert(Request $request)
    {
    	$data = $request -> except('_token');
        // dd($data);
         $this -> validate($request,[
            'linkname' => 'required|max:6|min:2',
            'url' => 'required',
            'order' => 'required|regex:[[0-9]+]',
        ],[
            'linkname.required' => '链接名称不能为空',
            'linkname.max' => '请输入2-6位的链接名称',
            'linkname.min' => '请输入2-6位的链接名称',
            'url.required' => '链接地址不能为空', 
            'order.required' => '排序值不能为空',
            'order.regex' => '排序值请输入数字',
        ]);

          //处理pic地址,存入数据库
       
       if($request -> hasFile('logo'))//判断是否有文件
       {
            if($request -> file('logo') -> isValid())//判断文件是否有效
            {
                //获取文件的后缀名
                $suffix = $request -> file('logo') -> getClientOriginalExtension();
                // echo $suffix;
                //制作文件名
                $fileName = time().rand().'.'.$suffix;
                //上传文件
                $path = './uploads';//上传文件的目录
                //执行上传
                $request -> file('logo') -> move($path,$fileName);
                //拼凑pic的URL地址
                $data['logo'] = trim($path.'/'.$fileName,'.');
                // return $data['logo'];
            }
       }

       //将数据发送到数据库
       $res = DB::table('bro_link') -> insert($data);

       //执行发送判断
       if($res)
       {
         return redirect('/admin/conf/link/show') -> with('success','友情链接添加成功');
       }else{
         return back() -> with('error','友情链接添加失败');
       }
    }


    public function edit(Request $request,$id)
    {
         $title= '友情链接编辑';

         //会话跟踪
         $staff = $request->session()->get('staff');

        //执行数据查询
        $data = DB::table('bro_link') -> where('id',$id) -> first();
        //解析模板
        return view('/admin/conf/link/edit',['title'=>$title,'data'=>$data,'staff' => $staff]);
    }


    public function update(Request $request)
    {
         $data = $request -> except('_token','oldlogo');

        //检测添加页面的数据并执行响应
         $this -> validate($request,[
            'linkname' => 'required|max:6|min:2',
            'url' => 'required',
            'order' => 'required|regex:[[0-9]+]',
        ],[
            'linkname.required' => '链接名称不能为空',
            'linkname.max' => '请输入2-6位的链接名称',
            'linkname.min' => '请输入2-6位的链接名称',
            'url.required' => '链接地址不能为空', 
            'order.required' => '排序值不能为空',
            'order.regex' => '排序值请输入数字',
        ]);
        //处理pic地址,存入数据库
       $oldpath = '.'.$request -> input('oldlogo');
       // dd($oldpath);
       if($request -> hasFile('logo'))//判断是否有文件
       {
            if($request -> file('logo') -> isValid())//判断文件是否有效
            {
                //获取文件的后缀名
                $suffix = $request -> file('logo') -> getClientOriginalExtension();
                // echo $suffix;
                //制作文件名
                $fileName = time().rand().'.'.$suffix;
                //上传文件
                $path = './uploads';//上传文件的目录

                //执行上传
                $request -> file('logo') -> move($path,$fileName);
                //拼凑pic的URL地址
                $data['logo'] = trim($path.'/'.$fileName,'.');

                if(file_exists($oldpath))
                {
                    unlink($oldpath);
                }

            }
       }
        // dd($data);
        // 发送修改语句
        $res = DB::table('bro_link') -> where('id',$data['id']) -> update($data);


        //判断修改是否成功
        if($res)
        {
            return redirect('/admin/conf/link/show') -> with('success','更新成功');
        }else{
            return back() -> with('error','更新失败');
        }
    }

    public function delete($id)
    {
        $logo = DB::table('bro_link')-> where('id',$id) -> first() -> logo;
        $oldpath = $logo;
                
        if(file_exists($oldpath))
        {  
              unlink($oldpath);
        }
        // dd($pic);
        $res = DB::table('bro_link')-> where('id',$id) -> delete();

        //判断删除是否成功
       if($res)
        {
            return redirect('/admin/conf/link/show') -> with('success','删除成功');
        }else{
            return back() -> with('error','删除失败');
        }
    }
    	
}
